%move background of absorption images

clc,clear
mode=1; %0 create the initial refdatabase, 1 roll in ref and calculate.
%need to remove background images
% time_photonum=xlsread('F:\spinTexture.xlsx','sheet1','465:582');
% YearMonthDay=time_photonum(:,1);
% photoStart0=time_photonum(:,2);
% photoEnd0=  time_photonum(:,3);
YearMonthDay=20161109;
photoStart0=[310];
photoEnd0=  [347];

%initial---------------------------------------------------------------------
photo_num=0;
refNum=250;
photo_length=length(photoStart0);
image_PSNR=cell(1,photo_length);
image_KBlur=cell(1,photo_length);


%----------------------------------------------------------------------

%folder filename and  of experiment data
% folderName=strcat('E:\�׳���\Rb87Files\',num2str(YearMonthDay),'morning\ikon_17639\');
% folderName='H:\photo\20161125morning\ikon-17639\';
photoNamePre='17639test_';
filename_pre='ascOD17639test_';
%filename and load address of reference images
refimagesIniDirectory='F:\Rb87Files\refimagesIni.mat';
%folder of Basic function 
% addpath(genpath('ExperimentBasicFunction'));
file_num=1;
while file_num<=length(photoStart0)
    serialn=1;
    row_num=1;
    photoStart=photoStart0(file_num);
    photoEnd=photoEnd0(file_num);
    image_length=photoEnd-photoStart+1;
    image_PSNR{1,file_num}=zeros(image_length,1);
    image_KBlur{1,file_num}=zeros(image_length,1);
    %folder filename and  of experiment data
    folderName=strcat('E:\�׳���\Rb87Files\',num2str(YearMonthDay(file_num)),'morning\');
    
    %--------------------------------------------------------------------------
    %before Remove background
    %files_if=[initialnumber finalnumber]
    files_if=[photoStart photoEnd];
    %if pictures includes 3 images,mode='3picture',else mode='1picture'
    figuremode='1picture';
    %define row and column number of subimage
    row_subimage=6;
    column_subimage=ceil(image_length/row_subimage);
    %load images from files
    [image]=drawpicture(folderName,filename_pre,files_if,figuremode);
    image_size=size(image);
    residue=row_subimage*column_subimage-image_size(2);
    pretreat_images(image,row_subimage,column_subimage);
    print( '-dpng','-r500', [folderName strcat('before remove background',num2str(photoStart),'-',num2str(photoEnd))]);
    
    
    %------------------------------------------------------------------------
    %store subimages
    %define size of figure
    figure('units','normalized','Name','After Remove background','position',[0.1,0.1,0.4,0.8]);
    %subimages= tight_subplot(row_subimage,column_subimage,[gap gap],[top bottom],[left right]);
    subimages=tight_subplot(row_subimage,column_subimage,[.005 .005],[.2 .1],[.05 .05]);
    picture_note('title','After Remove Background','xLabel','xpixels','yLabel','ypixels');
    
    %------------------------------------------------------------------------
    %find center of atoms
    % initial radial of region of cloud
    radial0=70;
    %edge value of atom cloud:edge0(1),find edge;edge0(2),define edge and final region of atoms cloud
    edge0=[0.05 0.05];
    %number of cloud in a picture
    cloudofnumber=2;
    %scan region
    x_initial=150;
    y_initial=150;
    x_final=600;
    y_final=600;
    %adjacent scan displacement
    scan_displacement=10;
    %distance between scanning circle and maxium of inside circle
    distance=2;
    [xpeak,ypeak,radial_cloud,image_size,average_background]=clouds_region(image,radial0,edge0,cloudofnumber,x_initial,y_initial,x_final,y_final,scan_displacement,distance);
    %--------------------------------------------------------------------------
    
  %load reference data base  
    refimagesIniStruct=load(refimagesIniDirectory);
    refimagesIni=refimagesIniStruct.refimagesIni;
    
    if size(refimagesIni,3)==refNum
        refimages=refimagesIni;
    else
        error('Error: refNum ~= loaded reference images');
    end
    
    
    bgmask2=ones(1024,1024);
    bgmask3=-ones(1024,1024);
    bgmask1=zeros(1024,1024);
    rectangle_FWHM=200;
    
    if mode==1
        photoInd=photoStart;
        while photoInd<=photoEnd
            %data input
            %           %roll in another ref image  there might be problem with the mod
            refimages(:,:,mod(photoInd-photoStart+photo_num,refNum)+1)=refCreate(photoInd,'bg',folderName,photoNamePre,1024);
            
            absimages=loadPhoto(photoName(folderName,photoNamePre,photoInd,'.asc'));
            nolimage=loadPhoto(photoName(folderName,photoNamePre,photoInd,'nolight.asc'));
            
            %calculate
            %             while radial<=radial_max
            if isempty(xpeak{1,serialn});photoInd=photoInd+1;serialn=serialn+1;continue;end
            xminROI1=80;yminROI1=130;
            [ROI ROI1]=rangeofinterest(xpeak{1,serialn},ypeak{1,serialn},radial_cloud{1,serialn},rectangle_FWHM,xminROI1,yminROI1);
            %change ROI and ROI1 by manual
            %                 ROI=[xmin,ymin,rectangle_width,rectangle_height];
            %                 ROI1=[xminROI1-30,yminROI1-30,800,800];
            ROIX_vector=ROI(1):(ROI(1)+ROI(3));
            ROIY_vector=ROI(2):(ROI(2)+ROI(4));
            ROIX_vector1=ROI1(1):(ROI1(1)+ROI1(3));
            ROIY_vector1=ROI1(2):(ROI1(2)+ROI1(4));
            bgmask3(ROIY_vector,ROIX_vector)=-2;
            bgmask1(ROIY_vector1,ROIX_vector1)=1;
            bgmask=bgmask1+bgmask2+bgmask3;
            %             figure(10),imagesc(bgmask)
            
            refimageCal=fringeremoval(absimages,refimages,bgmask);
            refimageCalMNolight=((refimageCal-nolimage)+abs(refimageCal-nolimage))/2+1;
            absimageMNolight=((absimages-nolimage)+abs(absimages-nolimage))/2+1;
            
            %                   figure(1);
            odimage=log(  refimageCalMNolight./absimageMNolight  );
            odimages(serialn).data=odimage;
            image_PSNR{1,file_num}(serialn,1)=imPSNR(image(serialn).data(ROIY_vector,ROIX_vector),odimage(ROIY_vector,ROIX_vector));
            image_KBlur{1,file_num}(serialn,1)=imKBlur(image(serialn).data,odimage);
            col_num=serialn-(row_num-1)*column_subimage;
            axes(subimages(serialn));
            imagesc(odimage,[-0.2,0.1]);
            row_num=show_axis(row_num,col_num,row_subimage,column_subimage,residue);
            serialn=serialn+1;
            
            title(num2str(photoInd));
            rectangle('position',ROI);
            %             print( '-dpng','-r500', [folderName num2str(photoInd)]);
            
            %save as mat files
            if photoInd<10
                savefilename=strcat(folderName,photoNamePre,'00',num2str(photoInd),'od.mat');%ͨ��ͳһ��������ʽ����ѭ�����㡣J:\SOCTc\F2013-02-23\RamanRabi
            else if (photoInd>=10)&&(photoInd<100)
                    savefilename=strcat(folderName,photoNamePre,'0',num2str(photoInd),'od.mat');
                else photoInd>=100;
                    savefilename=strcat(folderName,photoNamePre,num2str(photoInd),'od.mat');
                end
            end
            save(savefilename,'odimage');
            photoInd=photoInd+1;
        end
        save(refimagesIniDirectory,'refimagesIni');
        print( '-dpng','-r500', [folderName strcat('after remove background',num2str(photoStart),'-',num2str(photoEnd))]);
        photo_num=photo_num+serialn-1;
    end
    file_num=file_num+1;
end

